import { ref } from 'vue'
import { defineStore } from 'pinia'
import request from '@/api/request'
// 关于pinia的使用
export const useExamStore = defineStore('exam', () => {
    // 记录试卷分类
    const categories = ref<any>([])
    // 获取试卷分类数据
    async function getCategory() {
        await request.examCategoryList()
        .then(
            content=>{
                // console.log(content)
                let {code , result} = content
                if(code === 200) {
                    let newArr = result.map((item: any)=>{
                        item.icon = request.host+item.icon;
                        return item;
                    })
                    categories.value = newArr;
                }
            }
        )
    }

    // 定义试卷列表
    const exams = ref<any>([])


    // 定义要渲染的试卷列表
    const examList = ref<any>([])

    async function getExam() {
        await request.examListAll()
        .then(
            content=>{
                // console.log(content)
                let {code , result} = content;
                if(code == 200) {
                    exams.value = result;
                    examList.value = result;
                }
            }
        )
    }   
    // 筛选分类列表
    async function getCurrentCategoryList(id:string){
            if(exams.value) {
                let arr = exams.value.filter((item: any)=>{
                    return item.id == id;
                })
                examList.value = arr;
            }
    }

    // 初始化
    getCategory();
    getExam();
  
    return { categories, getCategory , exams ,examList, getExam , getCurrentCategoryList}
  })
  